﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Tianlongbabu
{
    /// <summary>
    /// 主要任务线
    /// </summary>
    public  class TreadMain
    {
        /// <summary>
        /// 任务队列
        /// </summary>
        public List<Task<string>> taskList = new List<Task<string>>();

        /// <summary>
        /// 故事开启
        /// </summary>
        public void Start()
        {
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            TaskFactory taskFactory = new TaskFactory();
            CancellationTokenSource cts = new CancellationTokenSource();


            taskFactory.StartNew(() =>
            {
                while (true)
                {
                    Thread.Sleep(1);
                    var num = new Random().Next(2000,2400);
                   
                    if (num == 2018)
                    {
                        Console.WriteLine($"{num}");
                        Console.WriteLine("天降雷霆灭世，天龙八部的故事就此结束....");
                        cts.Cancel();
                        break;
                    }

                }
            }, cts.Token);


            //乔峰
            taskList.Add(taskFactory.StartNew(() => {
                try
                {
                    Thread.Sleep(5000);
                    setstatus(() => { Console.WriteLine("乔峰是个丐帮帮主"); });
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }, cts.Token)
                .ContinueWith((obj) => {
                    try
                    {
                        Thread.Sleep(3400);
                        Console.WriteLine("乔峰变成契丹人");
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }
                  
                }, cts.Token)
                .ContinueWith((obj) => {

                    try
                    {
                        Thread.Sleep(2000);
                        Console.WriteLine("乔峰变成南苑大王");
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }
                  
                }, cts.Token)
                .ContinueWith<string>((obj) => {
                    try
                    {
                        Thread.Sleep(1000);
                        Console.WriteLine("乔峰挂印离开");
                        return "乔峰";
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                        return "";
                    }
                  
                }, cts.Token));

            //虚竹
            taskList.Add(taskFactory.StartNew(() => {
                try
                {
                    Thread.Sleep(3000);
                    setstatus(() => {
                        Console.WriteLine("虚竹是个小和尚");
                    });
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
                }, cts.Token)
                .ContinueWith((obj) => {

                    try
                    {
                        Thread.Sleep(2000);
                        Console.WriteLine("虚竹变成逍遥掌门");
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }

                  
                }, cts.Token)
                .ContinueWith((obj) => {
                    try
                    {
                        Thread.Sleep(2000);
                        Console.WriteLine("虚竹变成灵鹫宫宫主");
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }
                }, cts.Token)
                .ContinueWith<string>((obj) => {
                    try
                    {
                        Thread.Sleep(4000);
                        Console.WriteLine("虚竹变成西夏驸马");
                        return "虚竹";
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                        return "";
                    }

                   
                }, cts.Token));

            //段誉
            taskList.Add(taskFactory.StartNew(() => {
                try
                {
                    Thread.Sleep(2000);
                    setstatus(() => {
                        Console.WriteLine("段誉喜欢钟灵儿");
                    });
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
                }, cts.Token)
                .ContinueWith((obj) => {
                    try
                    {
                        Thread.Sleep(3000);
                        Console.WriteLine("段誉喜欢木婉清");
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }
                   
                }, cts.Token)
                .ContinueWith((obj) => {

                    try
                    {
                        Thread.Sleep(4300);
                        Console.WriteLine("段誉喜欢王语嫣");
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }
                }, cts.Token)
                .ContinueWith<string>((obj) => {

                    try
                    {
                        Thread.Sleep(5000);
                        Console.WriteLine("段誉变成大理国王");
                        return "段誉";
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                        return "";
                    }
                }, cts.Token));






            Task.WhenAny(taskList).ContinueWith((obj) => {
                try
                {
                    Console.WriteLine($"{ obj.Result.Result}已经做好准备!!!!!!!!");
                }
                catch(Exception e) {
                    Console.WriteLine(e.Message);
                }
            });
            Task.WhenAll(taskList).ContinueWith((obj) =>
            {
                Console.WriteLine($"中原群雄大战辽兵，忠义两难一死谢天");
                stopwatch.Start();
                Console.WriteLine($"整个故事用时{stopwatch.ElapsedMilliseconds}");
            });
        }

        /// <summary>
        /// 状态
        /// </summary>
        public bool status  = false;

        //锁
        private readonly Object TreadMain_test = new object();

        /// <summary>
        /// 锁内部的判断
        /// </summary>
        /// <param name="action"></param>
        public void setstatus(Action action)
        {
            lock (TreadMain_test)
            {
                if (!status)
                {
                    action.Invoke();
                    Console.WriteLine("天龙八部就此拉开序幕。。。。");
                    status = true;
                }
                else
                {
                    action.Invoke();
                }
            }
        }
    }
}
